package com.accloud.common;

import java.util.Random;

public class ACHelper {
    public static final String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private Random random = new Random();

    public ACSignature genSignature() {
        long timestamp = System.currentTimeMillis() / 1000;
        long timeout = (long) ACConfiguration.AUTH_TIMEOUT;
        String nonce = genNonce(timestamp, 16);
        return new ACSignature(timestamp, timeout, ACSigner.genSignature(ACSigner.genSignString(timestamp, timeout, nonce)).replace("0ffffff",""), nonce);
    }

    public ACSignature genRefreshSignature() {
        long timestamp = System.currentTimeMillis() / 1000;
        long timeout = (long) ACConfiguration.AUTH_TIMEOUT;
        String nonce = genNonce(timestamp, 16);
        return new ACSignature(timestamp, timeout, ACSigner.genSignature(ACSigner.genRefreshSignString(timestamp, timeout, nonce)), nonce);
    }

    public String genNonce(long seed, int length) {
        this.random.setSeed(seed);
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(base.charAt(this.random.nextInt(base.length())));
        }
        return sb.toString();
    }
}
